package com.chenjt.activiti.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chenjt.activiti.ResultModel;
import com.chenjt.activiti.entity.*;
import com.chenjt.activiti.utils.FlowUtils;
import com.chenjt.activiti.utils.StartBpmnUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 注释：
 *
 * @author chenjt
 * @date 2022/5/14 16:11
 */
@RestController
@RequestMapping("flow")
public class FlowController {

    private Logger log = LoggerFactory.getLogger(FlowController.class);


    /**
     * 启动流程实例
     * @param startProcessDefinitionEntity
     * @return
     */
    @PostMapping("/startProcessDefinition")
    public ResultModel startProcessDefinition(@RequestBody StartProcessDefinitionEntity startProcessDefinitionEntity){

        log.info("启动流程实例，参数为[{}]",startProcessDefinitionEntity.toString());

        String processDefinitionKey = startProcessDefinitionEntity.getProcessDefinitionKey();
        String businessKey = startProcessDefinitionEntity.getBusinessKey();
        Map<String, Object> variableMap = startProcessDefinitionEntity.getVariableMap();
        ResultModel resultModel = FlowUtils.startProcessDefinition(processDefinitionKey, businessKey, variableMap);

        log.info("启动流程实例结果：[{}]",resultModel.getStatus());

        return resultModel;
    }


    /**
     * 查询待办任务
     * @param assignee
     * @return
     */
    @GetMapping("/listMyTask")
    public ResultModel listMyTask(String assignee){

        log.info("查询用户[{}]待办任务",assignee);

        ResultModel resultModel = FlowUtils.listMyTask(assignee);

        Object data = resultModel.getData();
        String s = JSONObject.toJSONString(data);

        log.info("查询用户[{}]待办任务结果为[{}]",assignee,s);

        return resultModel;
    }

    /**
     * 完成任务
     * @param completeTaskWithCompleteEntity
     * @return
     */
    @PostMapping("/completeTaskWithComment")
    public ResultModel completeTaskWithComment(@RequestBody CompleteTaskWithCompleteEntity completeTaskWithCompleteEntity){

        log.info("完成任务，参数为[{}]",completeTaskWithCompleteEntity.toString());

        String businessKey = completeTaskWithCompleteEntity.getBusinessKey();

        String taskId = completeTaskWithCompleteEntity.getTaskId();

        String comment = completeTaskWithCompleteEntity.getComment();

        ResultModel resultModel = FlowUtils.completeTaskWithComment(businessKey, taskId, comment);

        Object data = resultModel.getData();
        String s = JSON.toJSONString(data);

        log.info("完成任务结果[{}]",s);

        return resultModel;
    }

    /**
     * 查询已办任务
     * @param assignee
     * @return
     */
    @GetMapping("/listCompleteTask")
    public ResultModel listCompleteTask(String assignee){

        log.info("查询用户[{}]已办任务",assignee);

        ResultModel resultModel = FlowUtils.listCompleteTask(assignee);

        Object data = resultModel.getData();
        String s = JSON.toJSONString(data);

        log.info("查询用户[{}]已办任务结果[{}]",assignee,s);

        return resultModel;
    }


    /**
     * 结束流程（撤回任务）
     * @param endTaskEntity
     * @return
     */
    @PostMapping("/endTask")
    public ResultModel endTask(@RequestBody EndTaskEntity endTaskEntity){

        log.info("结束流程,相关参数为[{}]",endTaskEntity.toString());

        String businessKey = endTaskEntity.getBusinessKey();
        String comment = endTaskEntity.getComment();

        ResultModel resultModel = FlowUtils.endTask(businessKey, comment);

        log.info("结束流程结果[{}]",resultModel.getStatus());

        return resultModel;

    }


    /**
     * 流程退回上个节点（撤回任务）
     * @param recallTaskEntity
     * @return
     */
    @PostMapping("/recallTask")
    public ResultModel recallTask(@RequestBody RecallTaskEntity recallTaskEntity){

        log.info("流程退回上个节点,相关参数为[{}]",recallTaskEntity.toString());

        String businessKey = recallTaskEntity.getBusinessKey();
        String taskId = recallTaskEntity.getTaskId();
        String comment = recallTaskEntity.getComment();

        ResultModel resultModel = FlowUtils.recallTask(businessKey, taskId, comment);

        log.info("流程退回上个节点[{}]",resultModel.getStatus());

        return resultModel;

    }

    /**
     * 通过businessKey查询该流程的所有历史审批节点和当前节点详情
     * @param businessKey
     * @return
     */
    @GetMapping("/listCompleteTaskByBusinessKey")
    public ResultModel listCompleteTaskByBusinessKey(String businessKey){

        log.info("查询流程[{}]所有审批节点详情",businessKey);

        ResultModel resultModel = FlowUtils.listCompleteTaskByBusinessKey(businessKey);

        Object data = resultModel.getData();
        String s = JSON.toJSONString(data);

        log.info("查询流程[{}]所有审批节点详情结果[{}]",businessKey,s);

        return resultModel;
    }


    /**
     * 任务交接
     * @param taskToOtherPersonEntity
     * @return
     */
    @PostMapping("/taskToOtherPerson")
    public ResultModel taskToOtherPerson(@RequestBody TaskToOtherPersonEntity taskToOtherPersonEntity){



        String fromUserId = taskToOtherPersonEntity.getFromUserId();
        String taskId = taskToOtherPersonEntity.getTaskId();
        String toUserId = taskToOtherPersonEntity.getToUserId();


        log.info("开始任务交接，taskId：[{}]，任务负责人：[{}]，交接给[{}]",taskId,fromUserId,toUserId);


        return FlowUtils.taskToOtherPerson(taskId,toUserId);

    }


    /**
     * 部署流程定义
     * @param classPathResource   224-inner(揭阳市储备粮食有限公司).bpmn20
     * @param processName      揭阳市储备粮食有限公司
     */
    @GetMapping("/deployProcessDefinition")
    public void deployProcessDefinition(String classPathResource,String processName){
        StartBpmnUtils.deployProcessDefinition(classPathResource,processName);
    }


    /**
     * 通过 businessKey 删除流程实例
     * @param businessKey
     */
    @GetMapping("/deleteProcessInstance")
    public void deleteProcessInstance(String businessKey){
        FlowUtils.deleteProcessInstance(businessKey,"");
    }

}
